#!/usr/bin/env jq
# compile-code-tsX_extractor -- Compiles tsv and tsj_extractors in the normalized JSON into code executable by the runner
##

include "constants";
include "sql";

.deepdive_ as $deepdive

| $deepdive.execution.processes | to_entries[]
| .value.name = .key | .value
| select(.style | test("ts[jv]_extractor"))
| (.style | rtrimstr("_extractor")) as $format
| {
    path: "\(.name)/run.sh",
    mode: "+x",
    content: "#!/usr/bin/env bash
# \(.style)  \(.name)
# \(@json)
set -xeuo pipefail
cd \"$(dirname \"$0\")\"

: ${DEEPDIVE_PREFIX_TABLE_TEMPORARY:=\(deepdivePrefixForTemporaryTables | @sh)} ${DEEPDIVE_PREFIX_TABLE_OLD:=\(deepdivePrefixForTemporaryOldTables | @sh)}

\(.before // "")

export DEEPDIVE_CURRENT_PROCESS_NAME=\(.name | @sh)
export DEEPDIVE_LOAD_FORMAT=\($format | @sh)

output_relation=\(.output_relation | @sh)
output_relation_tmp=\"${DEEPDIVE_PREFIX_TABLE_TEMPORARY}${output_relation}\"
output_relation_old=\"${DEEPDIVE_PREFIX_TABLE_OLD}${output_relation}\"

# use an empty temporary table as a sink instead of TRUNCATE'ing the output_relation
deepdive create table-if-not-exists \"$output_relation\"
deepdive create table \"$output_relation_tmp\" like \"$output_relation\"

# use compute driver to execute the UDF
deepdive compute execute \\
    input_sql=\(.input | asPrettySqlArg) \\
    command=\("cd \"$DEEPDIVE_APP\" && \(.udf)" | @sh) \\
    output_relation=\"$output_relation_tmp\" \\
    #

# replace with the new temporary table
: \"Replacing $output_relation with ${output_relation_tmp}\"
deepdive sql \"DROP TABLE IF EXISTS ${output_relation_old} CASCADE;\" 2>/dev/null || true
deepdive sql \"ALTER TABLE ${output_relation}     RENAME TO ${output_relation_old};\"
deepdive sql \"ALTER TABLE ${output_relation_tmp} RENAME TO ${output_relation};\"
deepdive sql \"DROP TABLE IF EXISTS ${output_relation_old} CASCADE;\" 2>/dev/null || true

# and analyze the table to speed up future queries
deepdive db analyze \"${output_relation}\"

\(.after // "")

deepdive mark \"done\" \(.name | @sh)
"
}
